<template>
  <div class="register">
    <!-- 注册页 -->
    <van-nav-bar title="注册" left-arrow @click-left="$router.back()" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' },
        { pattern: /^\w{5,}$/,message: '长度为5到8位,只能出现数字、字母、下划线'}]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' },
        { pattern: /^\w{5,}$/,message: '长度为5到8位,只能出现数字、字母、下划线'}]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>
    <div class="footer">
      <router-link class="btn" to="/login"
        >点我回首页 已有账号，去登录~</router-link
      >
    </div>
  </div>
</template>

<script>
import { registerApi } from '@/api/user.js'
export default {
  name: 'RegisterPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await registerApi(values)
      this.$toast.success(res.data.description)
    }
  }
}
</script>

<style lang="less">
.footer {
  font-size: 14px;
  text-align: center;
  .btn {
    color: #666;
  }
}
</style>
